package qianfeng.com.meijiadaka;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import qianfeng.com.meijiadaka.adapter.AllAdapter;
import qianfeng.com.meijiadaka.bean.AllBean;
import qianfeng.com.meijiadaka.interfacebean.AllData;
import qianfeng.com.meijiadaka.myview.MyLiveRecycleView;
import qianfeng.com.meijiadaka.myview.RecycleViewDivider;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class AllActivity extends AppCompatActivity {

    @BindView(R.id.all_ib)
    ImageButton mAllIb;
    @BindView(R.id.all_rv)
    MyLiveRecycleView mRv;
    private List<AllBean.ResultBean> mList;
    private String mBaseUri="http://home.naildaka.com/";
    private String mPath="http://home.naildaka.com/vapi/v4/home/picture/style/list";
    private AllAdapter mAllAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all);
        ButterKnife.bind(this);
        mList=new ArrayList<>();
        mRv.getRefreshableView().setLayoutManager(new GridLayoutManager(this,5, LinearLayoutManager.VERTICAL,false));
        mRv.getRefreshableView().addItemDecoration(new RecycleViewDivider(this, LinearLayout.VERTICAL,5, Color.WHITE));
        mRv.getRefreshableView().addItemDecoration(new RecycleViewDivider(this, LinearLayout.HORIZONTAL,5, Color.WHITE));
        mAllAdapter = new AllAdapter(this, mList);
        mRv.setAdapter(mAllAdapter);
        loadData();
    }

    private void loadData() {
        new Retrofit.Builder().baseUrl(mBaseUri).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build().create(AllData.class).getData(mPath).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<AllBean>() {
            @Override
            public void call(AllBean allBean) {
                mList.addAll(allBean.getResult());
                mAllAdapter.notifyDataSetChanged();
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Toast.makeText(AllActivity.this,"亲，网络不好",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick(R.id.all_ib)
    public void onClick() {
        finish();
    }
}
